package com.ruoyi.web.zangchat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.web.zangchat.domain.ChatAccount;
import com.ruoyi.web.zangchat.domain.ChatLogCash;
import com.ruoyi.web.zangchat.domain.dto.ChatUsersRechargeDTO;
import com.ruoyi.web.zangchat.service.IChatAccountService;
import com.ruoyi.web.zangchat.service.IChatLogCashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.zangchat.mapper.ChatUsersRechargeMapper;
import com.ruoyi.web.zangchat.domain.ChatUsersRecharge;
import com.ruoyi.web.zangchat.service.IChatUsersRechargeService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.security.Security;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户充值 服务层实现
 *
 * @author wudi
 * @date 2020-07-19
 */
@Service
public class ChatUsersRechargeServiceImpl extends ServiceImpl<ChatUsersRechargeMapper, ChatUsersRecharge> implements IChatUsersRechargeService
{
    @Autowired
    private IChatAccountService chatAccountService;
    @Autowired
    private IChatLogCashService chatLogCashService;
    @Override
    public List<Map<String, Object>> chatUsersRechargList(Map<String, Object> map) {
      return   baseMapper.chatUsersRechargList(map);
    }

    @Override
    @Transactional
    public AjaxResult editSave(ChatUsersRecharge chatUsersRecharge) {
        Long rechargeId = chatUsersRecharge.getRechargeId();
        ChatUsersRecharge oldChatUsersRecharge = baseMapper.selectById(rechargeId);
        Integer oldStatus = oldChatUsersRecharge.getStatus();
        if (oldStatus==1){
            return AjaxResult.error("该订单已审核通过，无法再次编辑");
        }
        Integer status = chatUsersRecharge.getStatus();
        if (status==1){
            ChatAccount chatAccount=new ChatAccount();
            Long userId = chatUsersRecharge.getUserId();
            chatAccount.setUserId(userId);
            QueryWrapper queryWrapper=new QueryWrapper<ChatAccount>(chatAccount);
            ChatAccount account = chatAccountService.getBaseMapper().selectOne(queryWrapper);
            BigDecimal preCash = account.getOrdinaryCash();
            BigDecimal cash = chatUsersRecharge.getCash();
            if (BigDecimal.ZERO.compareTo(cash)==1){
                return AjaxResult.error("输入金额有误，无法通过审核");
            }
            //插入流水
            ChatLogCash chatLogCash = chatLogCashService.initQlwShopLogCash(1, 0L, userId, chatUsersRecharge.getOrderNo(), preCash, 1, cash, "充值");
            chatLogCashService.getBaseMapper().insert(chatLogCash);
            //添加余额
            account.setOrdinaryCash(preCash.add(cash));
            chatAccountService.getBaseMapper().updateById(account);
        }
        chatUsersRecharge.setCreateTime(new Date());
        chatUsersRecharge.setCheckTime(new Date());
        Long checkStaffId = ShiroUtils.getUserId();
        chatUsersRecharge.setCheckStaffId(checkStaffId);
        baseMapper.updateById(chatUsersRecharge);

        return AjaxResult.success();
    }

    @Override
    public ChatUsersRechargeDTO selectChatUsersRechargDTOById(Long rechargeId) {
       return baseMapper.selectChatUsersRechargDTOById(rechargeId);

    }
}